package com.shelpe.services.sinbad.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.KeywordsController;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.mongo.KeywordAddErrorLog;
import com.shelpe.services.sinbad.entity.mongo.KeywordSupplementLog;
import com.shelpe.services.sinbad.model.Keyword;
import com.shelpe.services.sinbad.repository.mongo.KeywordAddErrorLogRepository;
import com.shelpe.services.sinbad.repository.mongo.KeywordSupplementLogRepository;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@Async("asyncExecutor")
public class SupplementService {
	
	@Autowired
	private OopKeywordService keywordService;
	@Autowired
	private KeywordSupplementLogRepository keywordSupplementLogRepository;
	@Autowired
	private KeywordAddErrorLogRepository keywordAddErrorLogRepository;

	@Async
	public void supplementAdd(Adgroup adgroup, List<KeywordsController.PricingKeyword> results, List<KeywordsController.PricingKeyword> originals){
		List<KeywordsController.PricingKeyword> supplements = new ArrayList<KeywordsController.PricingKeyword>();
		for(KeywordsController.PricingKeyword hk:originals){
			KeywordsController.PricingKeyword pk = this.filterByWord(results, hk.getWord());
			if(pk == null){
				supplements.add(hk);
			}
		}
		if(supplements.size() > 0){
			log.info("supplement to add {} words: {}", supplements.size(), adgroup);
			keywordService.add(adgroup, supplements, new OopKeywordService.Callback() {
				@Override
				public void call(List<Keyword> keywords) {
					log.info("success add to {} keywords: {}", keywords!=null?keywords.size():0, adgroup);
					KeywordSupplementLog sLog = new KeywordSupplementLog();
					sLog.from(adgroup);
					sLog.setResults(keywords);
					sLog.setSupplements(supplements);
					keywordSupplementLogRepository.save(sLog);
					
					if(keywords != null && keywords.size() > 0){
						List<KeywordAddErrorLog> addErrorLogs = keywordAddErrorLogRepository.findByAdgroupIDAndErrorCode(adgroup.getAutoID(), "请勿重复添加关键词");
						if(addErrorLogs != null && addErrorLogs.size() > 0){
							for(KeywordAddErrorLog addErrorLog: addErrorLogs){
								boolean allFixed = true;
								if(addErrorLog.getWords() != null){
									for(KeywordAddErrorLog.RecommendWord rw: addErrorLog.getWords()){
										Keyword fixed = filterKeywordByWord(keywords, rw.getWord());
										if(fixed != null){
											rw.setFixed(true);
										}
										allFixed = allFixed && rw.isFixed();
									}
								}
								if(allFixed){
									addErrorLog.setFixed(true);
								}
							}
							keywordAddErrorLogRepository.save(addErrorLogs);
						}
					}
				}
			}, false);
		}
	}
	
	private Keyword filterKeywordByWord(List<Keyword> keywords, String word){
		for(Keyword pk:keywords){
			if(pk.getWord().equals(word)){
				return pk;
			}
		}
		return null;
	}
	
	private KeywordsController.PricingKeyword filterByWord(List<KeywordsController.PricingKeyword> pricingKeywords, String word){
		for(KeywordsController.PricingKeyword pk:pricingKeywords){
			if(pk.getWord().equals(word)){
				return pk;
			}
		}
		return null;
	}
}
